<template>
  <div>
    <cus-modal
      ref="modal"
      title="食谱餐次所需领用食材"
      :width="1060"
      @onOk="onOk"
    >
      <div class="btnBox">
        <cusButton
          class="mr_20"
          type="primary"
          @onClick="getGoodsCreatereceivedtlAddAllReceiveDtlFromFoodBookApi()"
          style="width:150px;"
          >全部添加</cusButton
        >
      </div>
      <Table
        border
        :loading="form.loading"
        :columns="columns"
        :data="data"
        :size="vuex_config.tableSize"
        height="500"
      >
        <template slot-scope="{ row }" slot="operation">
          <div class="flex">
            <cus-button
              type="text"
              size="small"
              :style="{ color: '#1E6FFF' }"
              style="padding: 0 5px"
              @onClick="onAdd(row)"
            >
              添加领用
            </cus-button>
          </div>
        </template>
      </Table>
    </cus-modal>
    <!--添加全部接口错误提示-->
    <excelErrorModal ref="excelErrorModal"></excelErrorModal>
  </div>
</template>

<script>
import {
  postApiGoodsCreatereceivedtlSave,
  getGoodsCreatereceivedtlAddAllReceiveDtlFromFoodBook
} from "@/api/business/manager";
import excelErrorModal from "./excelErrorModal.vue";

const form = {
  productId: "", // 物资id
  unit: "", // 单位
  quantityStock: "", // 库存数量
  goodsTypeid: "", // 物资种类id
  price: "", // 价格
  recipientsNum: 1, // 领用重量-
  poinventroyId: "", // 库存 id值
  recipientsWay: "" // 领用方式
};
const formAll = {
  mealPlanId: "", // 食谱id
  mealTimes: "", // 餐次。0：早餐，1：中餐，2：晚餐，3：全天
  week: "" // 周次
};
const columns = [
  {
    title: "序号",
    type: "index",
    align: "center",
    minWidth: 70
  },
  {
    title: "物资名称",
    key: "productName",
    minWidth: 105,
    align: "center"
  },
  {
    title: "领用量",
    key: "recipientWeight",
    minWidth: 105,
    align: "center"
  },
  {
    title: "单位重量",
    key: "unitWeight",
    minWidth: 105,
    align: "center"
  },
  {
    title: "库存重量",
    key: "weight",
    minWidth: 105,
    align: "center",
    render: (h, params) => {
      return h("div", [h("span", {}, params.row.weight / 100)]);
    }
  },
  {
    title: "规格",
    key: "model",
    minWidth: 80,
    align: "center"
  },
  {
    title: "价格",
    key: "price",
    minWidth: 80,
    align: "center",
    render: (h, params) => {
      return h("div", [h("span", {}, params.row.price / 100)]);
    }
  },
  {
    title: "金额",
    key: "amount",
    minWidth: 105,
    align: "center",
    render: (h, params) => {
      return h("div", [h("span", {}, params.row.amount / 10000)]);
    }
  },
  {
    title: "操作",
    fixed: "right", // 右侧固定
    slot: "operation",
    align: "center",
    minWidth: 105
  }
];
export default {
  name: "",
  components: { excelErrorModal },
  data() {
    return {
      loading: false,
      form,
      formAll, // 提交全部按钮表单
      columns, // 表头
      data: [], // 列表数据
      pages: {
        page: 1,
        pageSize: 10,
        pageTotal: 0
      }
    };
  },
  mounted() {},
  methods: {
    show(res) {
      this.loading = false;
      this.$refs["modal"].show = true;
      this.data = res.data;
    },
    // 添加领用
    onAdd(row) {
      console.log(row);
      // 是否可以领用 0 否，1 是
      if (row.isRecipient == 0) {
        this.$Message.info(row.noRecipientMsg);
        console.log("0");
      } else {
        console.log("添加1");
        this.form = row;
        this.postApiGoodsCreatereceivedtlSaveApi();
      }
    },
    // 添加物资领用--------------------------接口
    postApiGoodsCreatereceivedtlSaveApi() {
      return new Promise(resolve => {
        let params = {
          productId: this.form.productId, // 物资id
          unit: this.form.unit, // 单位
          quantityStock: this.form.stockQty, // 库存数量
          goodsTypeid: this.form.goodsTypeId, // 物资种类id
          price: this.form.price * 1000, // 单价
          recipientsNum: this.form.recipientsNum * 1000, // 领用数量
          poinventroyId: this.form.id, // 库存 id值
          recipientsWay: this.form.recipientsWay // 领用方式
        };
        postApiGoodsCreatereceivedtlSave(params).then(res => {
          if (res.retCode == 2000) {
            this.$Message.success(res.retMsg);
          }
          resolve();
        });
      });
    },
    // 全部添加-----------------------------------------------接口
    getGoodsCreatereceivedtlAddAllReceiveDtlFromFoodBookApi() {
      return new Promise(resolve => {
        let params = {
          mealPlanId: this.formAll.mealPlanId,
          mealTimes: this.formAll.mealTimes,
          week: this.formAll.week
        };
        getGoodsCreatereceivedtlAddAllReceiveDtlFromFoodBook(params).then(
          res => {
            if (res.retCode == 2000) {
              this.$Message.success(res.retMsg);
            } else {
              if (res.data.length > 0) {
                this.$refs["excelErrorModal"].show();
                this.$refs["excelErrorModal"].data = res.data;
              }
            }
            resolve();
          }
        );
      });
    },
    onOk() {}
  }
};
</script>

<style scoped lang="less">
.btnBox {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
</style>
